package com.example.cpop.controller;

import com.example.cpop.mapper.LoveMusicMapper;
import com.example.cpop.model.Music;
import com.example.cpop.model.User;
import com.example.cpop.tools.Constant;
import com.example.cpop.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-10-18
 * Time: 16:45
 */

@RestController
@RequestMapping("/love")
public class LoveMusicController {


    @Autowired
    private LoveMusicMapper loveMusicMapper;


    //收藏喜欢的音乐
    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request){
        //HttpServletRequest request 验证是否登录成功
        //1.通过session检查是否登录成功
        int musicId = Integer.parseInt(id);
        System.out.println("musicId: " + musicId);
        //1.第一步查询音乐是否被收藏过 那么通过获取session获得userId
        HttpSession httpSession = request.getSession(false);//没有就不创建 有就获取
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录!");
            return new ResponseBodyMessage<>(-1, "请登录后上传", false);
        }

        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId: " + userId);

        Music music = loveMusicMapper.findLoveMusic(userId,musicId);
        if (music != null){
            //收藏过了  加一个取消收藏的功能
            return new ResponseBodyMessage<>(-1,"你之前收藏过这个音乐",false);
        }

        boolean effect = loveMusicMapper.insertLoveMusic(userId,musicId);
        if (effect){
            return new ResponseBodyMessage<>(0,"收藏成功",true);
        }else {
            return new ResponseBodyMessage<>(-1,"收藏失败",false);
        }
    }



    @RequestMapping("/findlovemusic")
    //查询喜欢的音乐
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,
                                                          HttpServletRequest request){

        HttpSession httpSession = request.getSession(false);//没有就不创建 有就获取
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录!");
            return new ResponseBodyMessage<>(-1, "请登录后查询", null);
        }

        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId: " + userId);

        List<Music> musicList = null;
        if (musicName == null){
            musicList = loveMusicMapper.findLoveMusicByUserId(userId);
        }else {
            musicList = loveMusicMapper.findLoveMusicBykeyAndUID(musicName,userId);
        }
        return new ResponseBodyMessage<>(1,"查询到了所有的歌曲信息",musicList);
    }

    //移除用户收藏的音乐
    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id,HttpServletRequest request){
        //id是一个字符串 先将他装换成int
        int musicId = Integer.parseInt(id);
        HttpSession httpSession = request.getSession(false);//没有就不创建 有就获取
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录!");
            return new ResponseBodyMessage<>(-1, "请登录后上传", false);
        }

        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId: " + userId);

        int ret = loveMusicMapper.deleteLoveMusic(userId,musicId);

        if (ret == 1){
            return new ResponseBodyMessage<>(0,"取消收藏成功",true);
        }else {
            return new ResponseBodyMessage<>(0,"取消收藏失败",false);

        }
    }


}
